#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include <unordered_map>
#include <algorithm>
#include <climits>
using namespace std;



typedef struct TreeNode
{
	struct TreeNode* left;
	struct TreeNode* right;
	char val;
	TreeNode(int value) : val(value), left(nullptr), right(nullptr) {}
	TreeNode() :left(nullptr), right(nullptr) {}
} TN;

class BTree
{
public:
	BTree() :root(nullptr) {};
	~BTree() {}

	void print()
	{
		find_longest_dis(root);
		sort(l.begin(), l.end());
		leftc = l.front().first;
		rightc = l.front().second;
		if (max_dis)
			cout << max_dis << ":" << min(leftc, rightc) << " " << max(leftc, rightc) << endl;
		else
			cout << max_dis << ":" << endl;
	}

	void creat(const string& s, int& i)
	{
		max_height = max_dis = 0;

		root = CreatTree(s, i);
		leftc = rightc = root->val;
	}

private:
	TN* root;
	int max_height, max_dis;
	char leftc, rightc, tempc;
	vector<pair<char, char>> l;
	vector<char> preback;


	TN* CreatTree(const string& s, int& i)
	{
		if (s[i] == '#')
		{
			i++;
			return nullptr;
		}
		TN* root = new TN;
		root->val = s[i++];
		root->left = CreatTree(s, i);
		root->right = CreatTree(s, i);
		return root;
	}

	void PreOrder(TN* r, int height)
	{
		if (r->left == nullptr && r->right == nullptr)
		{
			if (height > max_height)	
			{
				max_height = height;
				//tempc = r->val;
				preback.clear();
				preback.push_back(r->val);
			}
			else if (height == max_height)
			{
				max_height = height;

				preback.push_back(r->val);
			}
		}
		if (r->left != nullptr)
			PreOrder(r->left, height + 1);
		if (r->right != nullptr)
			PreOrder(r->right, height + 1);
	}

	void find_longest_dis(TN* r)
	{
		if (r == nullptr)
			return;
		int dis = 0;
		char a = r->val, b = r->val;
		if (r->left)
		{
			PreOrder(r->left, 1);

			//a = tempc;
			sort(preback.begin(), preback.end());
			a = preback.front();
			preback.clear();

		}

		dis += max_height;
		max_height = 0;
		if (r->right)
		{
			PreOrder(r->right, 1);
			//b = tempc;
			sort(preback.begin(), preback.end());
			b = preback.front();
			preback.clear();

		}

		dis += max_height;
		max_height = 0;

		if (dis == max_dis)
		{
			max_dis = dis;

			l.push_back(make_pair(min(a, b), max(a, b)));


			//if (leftc == root->val || leftc == r->val)
			//	leftc = a;
			//else
			//{
			//	leftc = min(leftc, a);
			//	//leftc = a;
			//	//fleftc = min(leftc, fleftc);
			//	//leftc = r->val;
			//}
			//	
			//if (rightc == root->val ||rightc == r->val)
			//	rightc = b;
			//else
			//{
			//	rightc = min(rightc, b);
			//	//rightc = b;
			//	//frightc = min(rightc, frightc);
			//	//rightc = r->val;
			//}
		}

		if (dis > max_dis)
		{
			max_dis = dis;

			l.clear();


			l.push_back(make_pair(min(a, b), max(a, b)));


			//if (leftc == root->val || leftc == r->val)
			//	leftc = a;
			//else
			//{
			//	leftc = min(leftc, a);
			//	//leftc = a;
			//	//fleftc = min(leftc, fleftc);
			//	//leftc = r->val;
			//}

			//if (rightc == root->val || rightc == r->val)
			//	rightc = b;
			//else
			//{
			//	rightc = min(rightc, b);
			//	//rightc = b;
			//	//frightc = min(rightc, frightc);
			//	//rightc = r->val;
			//}
		}


		find_longest_dis(r->left);
		find_longest_dis(r->right);
	}

	int TreeHight(TN* root)
	{
		if (root == nullptr)
		{
			return 0;
		}
		int leftTreeHeight = TreeHight(root->left);
		int rightTreeHeight = TreeHight(root->right);
		return leftTreeHeight > rightTreeHeight ? leftTreeHeight + 1 : rightTreeHeight + 1;
	}
};


void test6()
{
	int t;
	cin >> t;
	while (t--)
	{
		BTree bt;
		string s;
		int i = 0;
		cin >> s;
		bt.creat(s, i);

		bt.print();
	}

}

int main()
{
	//test1();
	//test2();
	//test3();
	//test5();
	test6();
	return 0;
}